#!/usr/bin/env bash
# SPDX-License-Identifier: BSD-3-Clause
# Copyright 2016-2019, Intel Corporation

#
# src/test/rpmem_basic/TEST8 -- unit test for rpmem create request
#                               in 2-to-1 configuration
#

. ../unittest/unittest.sh

require_test_type medium

. setup2to1.sh

setup

for node in 1 2; do
	replica=${REPLICA[${node}]}
	poolset_dir=${RPMEM_POOLSET_DIR[$replica]}
	part_dir=${PART_DIR[${node}]}

	create_poolset $DIR/pool0.set  8M:$part_dir/pool0.part0 8M:$part_dir/pool0.part1
	create_poolset $DIR/pool1.set 16M:$part_dir/pool1.part0
	create_poolset $DIR/pool2.set 16M:$part_dir/pool2.part0
	create_poolset $DIR/pool3.set  8M:$part_dir/pool3.part0 8M:$part_dir/pool3.part1
	# Pool set file with duplicated part file
	create_poolset $DIR/pool4.set  8M:$part_dir/pool3.part0 8M:$part_dir/pool4.part1
	# Pool set file for test of read operation
	create_poolset $DIR/pool5.set  8M:$part_dir/pool5.part0 8M:$part_dir/pool5.part1

	cp $DIR/pool0.set $DIR/pool0r.set

	run_on_node $replica "rm -rf $poolset_dir $part_dir && mkdir -p $poolset_dir/subdir && mkdir -p $part_dir"

	copy_files_to_node $replica $poolset_dir $DIR/{pool0.set,pool0r.set,pool1.set,pool3.set,pool4.set,pool5.set}
	copy_files_to_node $replica $poolset_dir/subdir/ $DIR/pool2.set

	# Remove read permission
	run_on_node $replica chmod a-r $poolset_dir/pool0r.set

	# Prepare pool5 for test of read and persist operations
	expect_normal_exit run_on_node ${node} ./rpmem_basic$EXESUFFIX\
		test_create 3 pool5.set ${REP_ADDR[${node}]} mem 8M none test_close 3

	expect_normal_exit run_on_node $replica ./rpmem_basic$EXESUFFIX\
		fill_pool $poolset_dir/pool5.set 1234

	ARGS=""
	#
	# CREATE
	#
	# Create pool from non-existing pool set file
	ARGS="$ARGS test_create 0 poolX.set ${REP_ADDR[${node}]} pool 8M none"
	# Create pool from pool set file which can't be read
	ARGS="$ARGS test_create 0 pool0r.set ${REP_ADDR[${node}]} pool 8M none"
	# Create too big pool
	ARGS="$ARGS test_create 0 pool0.set ${REP_ADDR[${node}]} pool 32M none"
	# Create pool and close it - local pool from file
	ARGS="$ARGS test_create 0 pool0.set ${REP_ADDR[${node}]} pool 8M none test_close 0"
	# Create already existing pool
	ARGS="$ARGS test_create 0 pool0.set ${REP_ADDR[${node}]} pool 8M none"
	# Create pool and close it - local pool from memory
	ARGS="$ARGS test_create 0 pool1.set ${REP_ADDR[${node}]} mem 8M none test_close 0"
	# Create pool from pool set with subdirectory
	ARGS="$ARGS test_create 0 subdir/pool2.set ${REP_ADDR[${node}]} mem 8M none test_close 0"
	# Create pool but don't close it immediately
	ARGS="$ARGS test_create 0 pool3.set ${REP_ADDR[${node}]} mem 8M none"
	# Create already created and not closed pool
	ARGS="$ARGS test_create 1 pool3.set ${REP_ADDR[${node}]} mem 8M none"
	# Create pool from pool set which has duplicated part file
	ARGS="$ARGS test_create 2 pool4.set ${REP_ADDR[${node}]} mem 8M none"
	# Close previously created pool
	ARGS="$ARGS test_close 0"

	#
	# OPEN
	#
	# Open pool from non-existing pool set file
	ARGS="$ARGS test_open 0 poolX.set ${REP_ADDR[${node}]} pool 8M init none"
	# Open pool from pool set file which can't be read
	ARGS="$ARGS test_open 0 pool0r.set ${REP_ADDR[${node}]} pool 8M init none"
	# Open too big pool
	ARGS="$ARGS test_open 0 pool0.set ${REP_ADDR[${node}]} pool 32M init none"
	# Open pool and close it - local pool from file
	ARGS="$ARGS test_open 0 pool0.set ${REP_ADDR[${node}]} pool 8M init none test_close 0"
	# Open pool and close it - local pool from memory
	ARGS="$ARGS test_open 0 pool1.set ${REP_ADDR[${node}]} mem 8M init none test_close 0"
	# Open pool from pool set with subdirectory
	ARGS="$ARGS test_open 0 subdir/pool2.set ${REP_ADDR[${node}]} pool 8M init none test_close 0"
	# Open pool but don't close it immediately
	ARGS="$ARGS test_open 0 pool3.set ${REP_ADDR[${node}]} pool 8M init none"
	# Open already opened and not closed pool
	ARGS="$ARGS test_open 1 pool3.set ${REP_ADDR[${node}]} pool 8M init none"
	# Open pool from pool set which has duplicated part file
	ARGS="$ARGS test_open 2 pool4.set ${REP_ADDR[${node}]} pool 8M init none"
	# Close previously created pool
	ARGS="$ARGS test_close 0"

	#
	# READ & PERSIST
	#
	ARGS="$ARGS test_open 3 pool5.set ${REP_ADDR[${node}]} pool 8M init none"
	ARGS="$ARGS test_read 3 1234"
	ARGS="$ARGS test_persist 3 4321 1 1 0"
	ARGS="$ARGS test_persist 3 4321 8 8 1"
	ARGS="$ARGS test_close 3"

	ARGS_NODE[${node}]=$ARGS

done

expect_normal_exit run_on_node_background 1 ${PID_FILE[1]} ./rpmem_basic$EXESUFFIX ${ARGS_NODE[1]}
expect_normal_exit run_on_node_background 2 ${PID_FILE[2]} ./rpmem_basic$EXESUFFIX ${ARGS_NODE[2]}

expect_normal_exit wait_on_node 1 ${PID_FILE[1]}
expect_normal_exit wait_on_node 2 ${PID_FILE[2]}

check

pass
